
#include <QXmlStreamReader>
#include <QFile>

#include "StatistiquesClient.h"

QList<StatistiquesClient> StatistiquesClient::GetStatistiquesClients()
{
	QList<StatistiquesClient> stats;
	QFile file("xml/stats.xml");

	if(file.open(QIODevice::ReadOnly | QIODevice::Text))
	{
		QXmlStreamReader reader(&file);
		while(!reader.atEnd())
		{
			QXmlStreamReader::TokenType token = reader.readNext();

			if(token == QXmlStreamReader::StartDocument)
				continue;

			if(token == QXmlStreamReader::StartElement)
			{
				stats.append(ParseStatistique(reader));
			}
		}

		file.close();
		reader.clear();
	}

	return stats;
}

StatistiquesClient StatistiquesClient::ParseStatistique(QXmlStreamReader& xml)
{
	StatistiquesClient s;
	
	xml.readNext();
	while(!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == "stat"))
	{
		QXmlStreamReader::TokenType token = xml.readNext();

		if(token == QXmlStreamReader::EndElement)
			continue;	// décoration

		if(xml.name() == "id")
		{
			xml.readNext();
			s.SetIdClient(xml.text().toString().toInt());
		}

		if(xml.name() == "totalRdv")
		{
			xml.readNext();
			s.SetNbTotalRendezVous(xml.text().toString().toInt());
		}

		if(xml.name() == "rdvEnAttente")
		{
			xml.readNext();
			s.SetNbRendezVousEnAttente(xml.text().toString().toInt());
		}

		if(xml.name() == "moyApparence")
		{
			xml.readNext();
			s.SetMoyApparence(xml.text().toString().toInt());
		}
		
		if(xml.name() == "moyHumour")
		{
			xml.readNext();
			s.SetMoyHumour(xml.text().toString().toInt());
		}

		if(xml.name() == "moyCharme")
		{
			xml.readNext();
			s.SetMoyCharme(xml.text().toString().toInt());
		}

		if(xml.name() == "moyIntelligence")
		{
			xml.readNext();
			s.SetMoyIntelligence(xml.text().toString().toInt());
		}

		if(xml.name() == "moyConversation")
		{
			xml.readNext();
			s.SetMoyConversation(xml.text().toString().toInt());
		}

		if(xml.name() == "moyGentillesse")
		{
			xml.readNext();
			s.SetMoyGentillesse(xml.text().toString().toInt());
		}

	}	

	return s;
}

void StatistiquesClient::SaveStats(QList<StatistiquesClient> stats)
{
	QFile file("xml/stats.xml");

	if(file.open(QIODevice::WriteOnly | QIODevice::Text))
	{
		QXmlStreamWriter* writer = new QXmlStreamWriter(&file);
		writer->setAutoFormatting(true);
		writer->writeStartDocument();
		writer->writeStartElement("stats");

		for(int i=0; i<stats.size(); i++)
			AddStatsClientXmlProperties(writer, stats.at(i));

		writer->writeEndElement();
		writer->writeEndDocument();
		file.close();
	}
}

void StatistiquesClient::AddStatsClientXmlProperties(QXmlStreamWriter* xml, StatistiquesClient s)
{
	xml->writeStartElement("stat");
	xml->writeTextElement("id", QString("%1").arg(s.GetIdClient()));	
	xml->writeTextElement("totalRdv", QString("%1").arg(s.GetNbTotalRendezVous()));	
	xml->writeTextElement("rdvEnAttente", QString("%1").arg(s.GetNbRendezVousEnAttente()));	
	xml->writeTextElement("moyApparence", QString("%1").arg(s.GetMoyApparence()));	
	xml->writeTextElement("moyHumour", QString("%1").arg(s.GetMoyHumour()));	
	xml->writeTextElement("moyCharme", QString("%1").arg(s.GetMoyCharme()));	
	xml->writeTextElement("moyIntelligence", QString("%1").arg(s.GetMoyIntelligence()));	
	xml->writeTextElement("moyConversation", QString("%1").arg(s.GetMoyConversation()));	
	xml->writeTextElement("moyGentillesse", QString("%1").arg(s.GetMoyGentillesse()));	
	xml->writeEndElement();
}

